package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper  setmealDishMapper;
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Long[] ids) {
        /**
         * 1.遍历数组获取id
         * 2.如果当前套餐是启售中的，则不能删除
         */
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.findById(id);
            if(setmealVO.getStatus()== StatusConstant.ENABLE){
                //当前菜品是启售中的，则不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //菜品可以删除
            setmealMapper.delete(id);
            setmealMapper.deleteByDishId(id);
        }
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        return setmealVO;
    }


    @Override
    public void update(SetmealDTO setmealDTO) {
        /**
         * 1.修改setmeal表(DishDTO-->Dish)
         * 2.删除setmeal_dish表中的当前菜品的口味数据
         * 3.重新插入setmeal表
         */
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //更新时间
        setmeal.setUpdateTime(LocalDateTime.now());
        setmealMapper.update(setmeal);
        setmealMapper.deleteByDishId(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal  seteal = Setmeal.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .build();
        setmealMapper.update(seteal);
    }

    @Override
    public void saveWithSetmeal(SetmealDTO setmealDTO) {
        //保存套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //保存套餐和菜品的关联关系
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()){
            setmealDish.setSetmealId(setmeal.getId());
            setmealMapper.insertDish(setmealDish);
        }
    }


}
